package cn.edu.ctbu.labtest.service.impl;

import cn.edu.ctbu.labtest.dao.StudentRepository;
import cn.edu.ctbu.labtest.domain.Student;
import cn.edu.ctbu.labtest.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class StudentImplService implements StudentService {
    @Autowired
    private StudentRepository studentRepository;

    /**
     * 只分页的查询
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Pageable pageable){
        return studentRepository.findAll(pageable);
    }

    /**
     * 分页，带过滤的查询
     */
    public Page<Student> findAll(Example<Student> student, Pageable pageable){
        return studentRepository.findAll(student,pageable);
    }


    /**
     * 读取所有的学生数据
     * @return
     */
    public List<Student> findAll(){
        return studentRepository.findAll();
    }

    /**
     * 插入学生
     * @param student
     * @return
     */
    public Student insert(Student student){
        return studentRepository.save(student);
    }

    /**
     * 更新学生信息
     * @param student
     * @return
     */
    public Student update(Student student){
        return studentRepository.save(student);
    }


    /**
     * 通过id查找学生
     * @param id
     * @return
     */
    public Student getById(Integer id){
        Student student=studentRepository.findById(id).orElse(null);
        return student;
    }

    /**
     * 通过id查询学生
     */
    public Optional<Student> findStudent(Integer id){
        return studentRepository.findById(id);
    }

    /**
     * 通过名字查询学生
     */
    public List<Student> findByName(String name){
        Student student=new Student();
        student.setName(name);
        return studentRepository.findByName(name);
    }

    /**
     * 通过名字模糊查询
     */
    public  Page<Student> findByNameLike(String name,Pageable pageable){
        Student student=new Student();
        student.setName(name);
        return studentRepository.findByNameLike(name, pageable);
    }

    /**
     * 通过名字和密码查询
     */
    public List<Student> findByNameAndPassword(String name,String password){
        return studentRepository.findByNameAndPassword(name, password);
    }

    /**
     * 删除学生
     */
    public void delete(Student student){
        studentRepository.delete(student);
    }
    public void delete(Integer id){
        Student student=new Student();
        student.setId(id);
        studentRepository.delete(student);
    }


    //    /**
//     * 通过名字和成绩查询
//     */
//    public List<Student> findByNameScore(String name,Integer score){
//        return studentRepository.findByNameScore(name, score);
//    }
//
//    /**
//     * 通过最高成绩查询
//     */
//    public List<Student> findByMaxAge(){
//        return studentRepository.findByMaxAge();
//    }

    public void batchRemove(Iterable<Integer> ids){
        studentRepository.deleteAllByIdInBatch(ids);
    }
}
